package com.cos.web.controller;

import com.cos.entity.Emp;
import com.cos.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: COS
 * @time: 2021/5/19 16:43
 * @description:
 */
@Controller
//@RestController
@RequestMapping("/json")
public class Demo4Controller {

    @RequestMapping("/d1")
    @ResponseBody
    public String d1(){
        //直接写出字符串
        return "OK";
    }

    @RequestMapping("/d2")
    @ResponseBody
    public User d2(){
        User u = new User();
        u.setName("张三");
        u.setSex("男");
        return u;
    }
    @RequestMapping("/d3")
    @ResponseBody
    public List<User> d3(){
        User u = new User();
        u.setName("张三");
        u.setSex("男");
        u.setBornDate(new Date());
        User u2 = new User();
        u2.setName("张三2");
        u2.setSex("男2");
        u2.setBornDate(new Date());
        List<User> ul = new ArrayList<>();
        ul.add(u);
        ul.add(u2);
        return ul;
    }
    /**
     *
     * @author: COS
     * @return:  
     * @time: 2021/5/19 17:09
     * @description:  @ResponseBody 响应json
     *  @RequestBody 接受json数据
     */    
    @RequestMapping("/d4")
    @ResponseBody
    public String d4(@RequestBody Emp u ){
        System.out.println(u);
        return "ok";
    }

}
